# Plotting Functions

# Interactive/Marginal Effects

coef_plots_multivar <- function(etab, iv){
  
  out <- etab %>% data.frame()
  colnames(out) <- out[1,] 
  colnames(out)[1] <- "var_name"
  ideo <- out[2,][2] %>% unlist
  
  nyears <- out %>% filter(var_name == "Observations") %>% t() %>% data.frame() %>% 
    mutate(year = row.names(.)) 
  colnames(nyears) <- c("Observations", "year")
  nyears <- nyears[-c(1),]
  nyears$Observations <- as.numeric(str_replace_all(nyears$Observations, ",", ""))
  
  out_clean <- out %>% 
    pivot_longer(!var_name, names_to = "year", values_to = "values") %>% 
    filter(var_name %in% iv) %>% 
    separate(values, into = c("coef", "se"), " ") %>% 
    mutate(coef = str_replace_all(coef, " ", ""), 
           coef = str_replace_all(coef, "\\*", ""), 
           se = str_replace_all(se, "\\(", ""), 
           se = str_replace_all(se, "\\)", "")) %>% 
    mutate(across(c(coef, se), as.numeric)) %>% 
    left_join(nyears)
  
  out_clean %>% 
    ggplot(., aes(x = as.factor(year), col = var_name)) + 
    geom_hline(aes(yintercept = 0), col = "red") +
    geom_point(aes(y = coef), position = position_dodge(width = 1)) +
    geom_linerange(aes(ymin=coef - (qt(0.975, Observations)*se), ymax=coef + (qt(0.975, Observations)*se)), position = position_dodge(width = 1), size = 0.5) +
    geom_linerange(aes(ymin=coef - (qt(0.95, Observations)*se), ymax=coef + (qt(0.95, Observations)*se)), position = position_dodge(width = 1), size = 0.75) +
    xlab("Election Year") + ylab("Coefficient") + ggtitle(ideo) + 
    theme_light()
  
}

# Dummy/Faceted Effects

coef_plots_facetvar <- function(etab, iv){
  
  out <- etab %>% data.frame()
  colnames(out) <- out[1,] 
  colnames(out)[1] <- "var_name"
  ideo <- out[2,][2] %>% unlist
  
  nyears <- out %>% filter(var_name == "Observations") %>% t() %>% data.frame() %>% 
    mutate(year = row.names(.)) 
  colnames(nyears) <- c("Observations", "year")
  nyears <- nyears[-c(1),]
  nyears$Observations <- as.numeric(str_replace_all(nyears$Observations, ",", ""))
  
  out_clean <- out %>% 
    pivot_longer(!var_name, names_to = "year", values_to = "values") %>% 
    filter(var_name %in% iv) %>% 
    separate(values, into = c("coef", "se"), " ") %>% 
    mutate(coef = str_replace_all(coef, " ", ""), 
           coef = str_replace_all(coef, "\\*", ""), 
           se = str_replace_all(se, "\\(", ""), 
           se = str_replace_all(se, "\\)", "")) %>% 
    mutate(across(c(coef, se), as.numeric)) %>% 
    left_join(nyears)
  
  out_clean %>% 
    mutate(Confs_facet = ifelse(str_detect(var_name, "sme_intense1-4"), "Sporadic Mobilization", NA), 
           Confs_facet = ifelse(str_detect(var_name, "sme_intense5+"), "Endemic Mobilization", Confs_facet), 
           Confs_facet = ifelse(str_detect(var_name, "sme_intense0"), "No Moblization", Confs_facet)) %>% 
    mutate(CdP_status = ifelse(str_detect(var_name, "as.factor\\(early_cdp\\)1"), "CdP Present", "No CdP Present"), 
           CdP_status = ifelse(str_detect(var_name, "as.factor\\(early_cdp\\)0"), "No CdP Present", CdP_status), 
           CdP_status = factor(CdP_status, levels = c("No CdP Present", "CdP Present"))) %>% 
    ggplot(., aes(x = as.factor(year), col = CdP_status)) + 
    geom_hline(aes(yintercept = 0), col = "red") +
    geom_point(aes(y = coef), position = position_dodge(width = 1)) +
    geom_linerange(aes(ymin=coef - (qt(0.975, Observations)*se), ymax=coef + (qt(0.975, Observations)*se)), position = position_dodge(width = 1), size = 0.5) +
    geom_linerange(aes(ymin=coef - (qt(0.95, Observations)*se), ymax=coef + (qt(0.95, Observations)*se)), position = position_dodge(width = 1), size = 0.75) +
    xlab("Election Year") + ylab("Coefficient") + ggtitle(ideo) + facet_wrap(~ Confs_facet) +
    theme_light()
  
}

# Dummy/Faceted Effects, Just PCP

coef_plots_facetvar_justpcp <- function(etab, iv){
  
  out <- etab %>% data.frame()
  colnames(out) <- out[1,] 
  colnames(out)[1] <- "var_name"
  ideo <- out[2,][2] %>% unlist
  
  nyears <- out %>% filter(var_name == "Observations") %>% t() %>% data.frame() %>% 
    mutate(year = row.names(.)) 
  colnames(nyears) <- c("Observations", "year")
  nyears <- nyears[-c(1),]
  nyears$Observations <- as.numeric(str_replace_all(nyears$Observations, ",", ""))
  
  out_clean <- out %>% 
    pivot_longer(!var_name, names_to = "year", values_to = "values") %>% 
    filter(var_name %in% iv) %>% 
    separate(values, into = c("coef", "se"), " ") %>% 
    mutate(coef = str_replace_all(coef, " ", ""), 
           coef = str_replace_all(coef, "\\*", ""), 
           se = str_replace_all(se, "\\(", ""), 
           se = str_replace_all(se, "\\)", "")) %>% 
    mutate(across(c(coef, se), as.numeric)) %>% 
    left_join(nyears)
  
  out_clean %>% 
    mutate(Confs_facet = ifelse(str_detect(var_name, "sme_intense1-4"), "Sporadic Mobilization", NA), 
           Confs_facet = ifelse(str_detect(var_name, "sme_intense5+"), "Endemic Mobilization", Confs_facet), 
           Confs_facet = ifelse(str_detect(var_name, "sme_intense0"), "No Moblization", Confs_facet)) %>% 
    mutate(CdP_status = ifelse(str_detect(var_name, "as.factor\\(early_cdp\\)1"), "CdP Present", "No CdP Present"), 
           CdP_status = ifelse(str_detect(var_name, "as.factor\\(early_cdp\\)0"), "No CdP Present", CdP_status), 
           CdP_status = factor(CdP_status, levels = c("No CdP Present", "CdP Present"))) %>% 
    filter(Confs_facet == "Endemic Mobilization") %>% 
    ggplot(., aes(x = as.factor(year), col = CdP_status)) + 
    geom_hline(aes(yintercept = 0), col = "red") +
    geom_point(aes(y = coef), position = position_dodge(width = 1)) +
    geom_linerange(aes(ymin=coef - (qt(0.975, Observations)*se), ymax=coef + (qt(0.975, Observations)*se)), position = position_dodge(width = 1), size = 0.5) +
    geom_linerange(aes(ymin=coef - (qt(0.95, Observations)*se), ymax=coef + (qt(0.95, Observations)*se)), position = position_dodge(width = 1), size = 0.75) +
    xlab("Election Year") + ylab("Coefficient") + ggtitle(ideo) + facet_wrap(~ Confs_facet) +
    theme_light()
  
}

# Dummy/Faceted Effects, Alternative Coding

coef_plots_facetvar_alt <- function(etab, iv){
  
  out <- etab %>% data.frame()
  colnames(out) <- out[1,] 
  colnames(out)[1] <- "var_name"
  ideo <- out[2,][2] %>% unlist
  
  nyears <- out %>% filter(var_name == "Observations") %>% t() %>% data.frame() %>% 
    mutate(year = row.names(.)) 
  colnames(nyears) <- c("Observations", "year")
  nyears <- nyears[-c(1),]
  nyears$Observations <- as.numeric(str_replace_all(nyears$Observations, ",", ""))
  
  out_clean <- out %>% 
    pivot_longer(!var_name, names_to = "year", values_to = "values") %>% 
    filter(var_name %in% iv) %>% 
    separate(values, into = c("coef", "se"), " ") %>% 
    mutate(coef = str_replace_all(coef, " ", ""), 
           coef = str_replace_all(coef, "\\*", ""), 
           se = str_replace_all(se, "\\(", ""), 
           se = str_replace_all(se, "\\)", "")) %>% 
    mutate(across(c(coef, se), as.numeric)) %>% 
    left_join(nyears)
  
  out_clean %>% 
    mutate(Confs_facet = ifelse(str_detect(var_name, "sme_intense_alt1-2"), "Sporadic Mobilization", NA), 
           Confs_facet = ifelse(str_detect(var_name, "sme_intense_alt3+"), "Endemic Mobilization", Confs_facet), 
           Confs_facet = ifelse(str_detect(var_name, "sme_intense_alt0"), "No Moblization", Confs_facet)) %>% 
    mutate(CdP_status = ifelse(str_detect(var_name, "as.factor\\(early_cdp\\)1"), "CdP Present", "No CdP Present"), 
           CdP_status = ifelse(str_detect(var_name, "as.factor\\(early_cdp\\)0"), "No CdP Present", CdP_status), 
           CdP_status = factor(CdP_status, levels = c("No CdP Present", "CdP Present"))) %>% 
    ggplot(., aes(x = as.factor(year), col = CdP_status)) + 
    geom_hline(aes(yintercept = 0), col = "red") +
    geom_point(aes(y = coef), position = position_dodge(width = 1)) +
    geom_linerange(aes(ymin=coef - (qt(0.975, Observations)*se), ymax=coef + (qt(0.975, Observations)*se)), position = position_dodge(width = 1), size = 0.5) +
    geom_linerange(aes(ymin=coef - (qt(0.95, Observations)*se), ymax=coef + (qt(0.95, Observations)*se)), position = position_dodge(width = 1), size = 0.75) +
    xlab("Election Year") + ylab("Coefficient") + ggtitle(ideo) + facet_wrap(~ Confs_facet) +
    theme_light()
  
}

# Multivar All ideologies

coef_plots_multivar_allideos_long <- function(modeln, iv){

  ideo_holder<-list()
  for (k in 1:length(ideology)){

    out <- etable(lapply(ideology[[k]], `[[`, modeln), vcov = "cluster", signif.code =  c("***" = 0.01, "**" = 0.05, "*" = 0.10), tex = F,
                  headers = c("1975", "1976", "1979", "1980", "1983", "1985", "1987", "1991", "1995", "1999", "2002", "2005", "2009", "2011")) %>% data.frame()

    colnames(out) <- out[1,]
    colnames(out)[1] <- "var_name"
    ideo <- out[2,][2] %>% unlist

    nyears <- out %>% filter(var_name == "Observations") %>% t() %>% data.frame() %>%
      mutate(year = row.names(.))
    colnames(nyears) <- c("Observations", "year")
    nyears <- nyears[-c(1),]
    nyears$Observations <- as.numeric(str_replace_all(nyears$Observations, ",", ""))

    ideo_holder[[k]] <- out %>%
      pivot_longer(!var_name, names_to = "year", values_to = "values") %>%
      filter(var_name %in% iv) %>%
      separate(values, into = c("coef", "se"), " ") %>%
      mutate(coef = str_replace_all(coef, " ", ""),
             coef = str_replace_all(coef, "\\*", ""),
             se = str_replace_all(se, "\\(", ""),
             se = str_replace_all(se, "\\)", "")) %>%
      mutate(across(c(coef, se), as.numeric)) %>%
      left_join(nyears) %>% mutate(ideo = ideo)

  }

  do.call(rbind, ideo_holder) %>%
    filter(ideo != "Left") %>% mutate(ideo = factor(ideo, c("Communist", "Center", "Right"))) %>%
    ggplot(., aes(x = as.factor(year), col = var_name)) +
    geom_hline(aes(yintercept = 0), col = "red") +
    geom_point(aes(y = coef), position = position_dodge(width = 1)) +
    geom_linerange(aes(ymin=coef - (qt(0.975, Observations)*se), ymax=coef + (qt(0.975, Observations)*se)), position = position_dodge(width = 1), size = 0.5) +
    geom_linerange(aes(ymin=coef - (qt(0.95, Observations)*se), ymax=coef + (qt(0.95, Observations)*se)), position = position_dodge(width = 1), size = 0.75) +
    xlab("Election Year") + ylab("Coefficient") + facet_wrap(~ideo) +
    theme_light()

}

